package svc

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/rest"
	"mzpd_server/internal/config"
	"mzpd_server/internal/middleware"
	"mzpd_server/models/dept_rooms"
	"mzpd_server/models/group_items"
	"mzpd_server/models/player_queues"
	"mzpd_server/models/queue_groups"
	"mzpd_server/models/queue_item"
	"mzpd_server/models/queue_seqnumber"
	"mzpd_server/models/registereds"
)

type ServiceContext struct {
	Config              config.Config
	HisPushMiddleware   rest.Middleware
	SqlCon              sqlx.SqlConn
	RegisteredsModel    registereds.RegisteredsModel
	PlayerQueuesModel   player_queues.PlayerQueuesModel
	QueueGroupsModel    queue_groups.QueueGroupsModel
	QueueSeqnumberModel queue_seqnumber.QueueSeqnumberModel
	DeptRoomsModel      dept_rooms.DeptRoomsModel
	GroupItemsModel     group_items.GroupItemsModel
	QueueItemModel      queue_item.QueueItemModel

	Redis *redis.Redis
}

func NewServiceContext(c config.Config) *ServiceContext {
	sqlConn := sqlx.NewMysql(c.MySql.DataSource)
	newRedis, err := redis.NewRedis(redis.RedisConf{
		Host:        c.RedisHost,
		Type:        "node",
		Pass:        "",
		Tls:         false,
		NonBlock:    false,
		PingTimeout: 0,
	})
	if err != nil {
		fmt.Printf("radis 失败:%s", err.Error())
	}
	return &ServiceContext{
		Redis:               newRedis,
		Config:              c,
		HisPushMiddleware:   middleware.NewHisPushMiddleware().Handle,
		SqlCon:              sqlConn,
		RegisteredsModel:    registereds.NewRegisteredsModel(sqlConn, c.RedisConf),
		PlayerQueuesModel:   player_queues.NewPlayerQueuesModel(sqlConn, c.RedisConf),
		QueueGroupsModel:    queue_groups.NewQueueGroupsModel(sqlConn, c.RedisConf),
		QueueSeqnumberModel: queue_seqnumber.NewQueueSeqnumberModel(sqlConn, c.RedisConf),
		DeptRoomsModel:      dept_rooms.NewDeptRoomsModel(sqlConn, c.RedisConf),
		GroupItemsModel:     group_items.NewGroupItemsModel(sqlConn, c.RedisConf),
		QueueItemModel:      queue_item.NewQueueItemModel(sqlConn, c.RedisConf),
	}
}
